<template>
  <div class="theme-toggle-wrapper">
    <el-tooltip
      :content="isDark ? '切换到浅色模式' : '切换到深色模式'"
      placement="bottom"
      :show-after="500"
    >
      <el-button
        :icon="isDark ? Sunny : Moon"
        circle
        size="large"
        @click="toggleTheme"
        class="theme-toggle"
        :class="{ 'dark-mode': isDark }"
      />
    </el-tooltip>
  </div>
</template>

<script setup lang="ts">
import { computed, watch } from 'vue'
import { Sunny, Moon } from '@element-plus/icons-vue'
import { useAppStore } from '@/stores/app'

const appStore = useAppStore()

const isDark = computed(() => appStore.isDark)

const toggleTheme = () => {
  // 添加切换动画效果
  document.documentElement.style.transition = 'all 0.3s ease'

  appStore.toggleTheme()

  setTimeout(() => {
    document.documentElement.style.transition = ''
  }, 300)
}

// 监听主题变化，更新页面样式
watch(isDark, (newValue) => {
  const root = document.documentElement

  if (newValue) {
    root.classList.add('dark')
    // 更新Element Plus CSS变量 - 使用新的深色主题颜色 #2c2f3b
    root.style.setProperty('--el-bg-color', '#2c2f3b')
    root.style.setProperty('--el-bg-color-page', '#242732')
    root.style.setProperty('--el-text-color-primary', '#ffffff')
    root.style.setProperty('--el-text-color-regular', '#e5e5e5')
    root.style.setProperty('--el-border-color', '#4a4e5f')
    root.style.setProperty('--el-border-color-light', '#525670')
    root.style.setProperty('--el-fill-color-light', '#363a4b')
    root.style.setProperty('--el-color-primary', '#409eff')
    root.style.setProperty('--el-color-warning', '#c67915')
    root.style.setProperty('--el-color-danger', '#c35c5d')
    root.style.setProperty('--el-color-error', '#7c191e')
  } else {
    root.classList.remove('dark')
    // 恢复默认CSS变量
    root.style.setProperty('--el-bg-color', '#ffffff')
    root.style.setProperty('--el-bg-color-page', '#f2f3f5')
    root.style.setProperty('--el-text-color-primary', '#303133')
    root.style.setProperty('--el-text-color-regular', '#606266')
    root.style.setProperty('--el-border-color', '#dcdfe6')
    root.style.setProperty('--el-border-color-light', '#e4e7ed')
    root.style.setProperty('--el-fill-color-light', '#f5f7fa')
    root.style.setProperty('--el-color-primary', '#409eff')
    root.style.setProperty('--el-color-warning', '#e6a23c')
    root.style.setProperty('--el-color-danger', '#f56c6c')
    root.style.setProperty('--el-color-error', '#f56c6c')
  }
}, { immediate: true })
</script>

<style lang="scss" scoped>
.theme-toggle-wrapper {
  display: inline-block;

  .theme-toggle {
    width: 44px;
    height: 44px;
    background: rgba(255, 255, 255, 0.9);
    border: 1px solid rgba(0, 0, 0, 0.08);
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    backdrop-filter: blur(8px);
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    position: relative;
    overflow: hidden;

    &::before {
      content: '';
      position: absolute;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
      background: linear-gradient(135deg, rgba(59, 130, 246, 0.1), rgba(147, 197, 253, 0.1));
      opacity: 0;
      transition: opacity 0.3s ease;
    }

    &:hover {
      transform: translateY(-2px) scale(1.05);
      box-shadow: 0 8px 20px rgba(0, 0, 0, 0.15);
      border-color: rgba(59, 130, 246, 0.3);

      &::before {
        opacity: 1;
      }
    }

    &:active {
      transform: translateY(0) scale(0.98);
    }

    &.dark-mode {
      background: rgba(44, 47, 59, 0.9);  // 使用 #2c2f3b
      border-color: rgba(255, 255, 255, 0.1);
      color: #ffffff;

      &::before {
        background: linear-gradient(135deg, rgba(198, 121, 21, 0.1), rgba(195, 92, 93, 0.1));  // 使用辅助颜色
      }

      &:hover {
        background: rgba(54, 58, 75, 0.9);  // 基于 #2c2f3b 的悬停色
        border-color: rgba(198, 121, 21, 0.3);  // 使用 #c67915
        box-shadow: 0 8px 20px rgba(0, 0, 0, 0.4);
      }
    }

    :deep(.el-icon) {
      font-size: 20px;
      transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
      position: relative;
      z-index: 1;
    }

    &:hover :deep(.el-icon) {
      transform: rotate(180deg) scale(1.1);
    }

    // 添加涟漪效果
    &::after {
      content: '';
      position: absolute;
      top: 50%;
      left: 50%;
      width: 0;
      height: 0;
      border-radius: 50%;
      background: rgba(59, 130, 246, 0.3);
      transform: translate(-50%, -50%);
      transition: width 0.3s ease, height 0.3s ease;
    }

    &:active::after {
      width: 100px;
      height: 100px;
    }

    &.dark-mode::after {
      background: rgba(198, 121, 21, 0.3);  // 使用 #c67915
    }
  }
}

/* 全局主题切换动画 */
:global(html) {
  transition: background-color 0.3s ease, color 0.3s ease;
}

:global(html.dark) {
  color-scheme: dark;
}

:global(html:not(.dark)) {
  color-scheme: light;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .theme-toggle-wrapper {
    .theme-toggle {
      width: 40px;
      height: 40px;

      :deep(.el-icon) {
        font-size: 18px;
      }
    }
  }
}

@media (max-width: 480px) {
  .theme-toggle-wrapper {
    .theme-toggle {
      width: 36px;
      height: 36px;

      :deep(.el-icon) {
        font-size: 16px;
      }
    }
  }
}
</style>
